package com.zbiti.anvil.basic.admin.gateway.selenium.page.system;

import com.zbiti.anvil.basic.admin.gateway.selenium.util.ElementUtils;
import com.zbiti.anvil.basic.admin.gateway.selenium.util.UiAssertHelper;
import com.zbiti.anvil.basic.admin.gateway.selenium.util.UiTriggerHelper;
import org.openqa.selenium.WebElement;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * 系统字典管理测试
 *
 * @author Fanyc
 * @date 2021-05-14 13:58
 */
public class SystemDictTester {

    /**
     * 查询区域
     */
    private final WebElement searchForm;

    /**
     * 工具栏
     */
    private final WebElement toolbar;

    /**
     * 查询结果表
     */
    private final WebElement table;

    /**
     * 分页器
     */
    private final WebElement pagination;

    /**
     * 加载遮罩
     */
    private final WebElement tableMask;

    /**
     * 样例数据
     */
    public static final String dictName = "Selenium字典";
    /**
     * 样例数据
     */
    public static final String dictCode = "selenium";

    /**
     * 构造函数
     */
    public SystemDictTester() {
        WebElement container = ElementUtils.cssSelector(null, "div[class='app-container']");
        this.searchForm = ElementUtils.cssSelector(container, "form[class~='el-form']");
        this.toolbar = ElementUtils.xpath(container, "div[1]");
        this.table = ElementUtils.cssSelector(container, "div[class~='el-table']");
        this.pagination = ElementUtils.cssSelector(container, "div[class~='pagination-container']");
        this.tableMask = ElementUtils.cssSelector(this.table, "div[class='el-loading-mask']");
    }

    /**
     * 执行查询
     *
     * @param dictName 字典名称
     * @param dictCode 字典编码
     * @param total    结果总数
     * @param pages    结果页数
     * @param reset    重置查询
     * @throws InterruptedException Sleep异常
     */
    private void search(String dictName, String dictCode, int total, int pages, boolean reset) throws InterruptedException {
        // 2.执行条件查询：字典名称=任务，字典类型=job，状态=正常
        ElementUtils.sendKeysByCssSelector(this.searchForm, "input[placeholder='请输入字典名称']", dictName);
        ElementUtils.sendKeysByCssSelector(this.searchForm, "input[placeholder='请输入字典类型']", dictCode);
        WebElement elSelect = ElementUtils.cssSelector(this.searchForm, "div[class~='el-select']");
        UiTriggerHelper.elSelectChange(elSelect, 0);
        UiTriggerHelper.clickSearchButton(this.searchForm, this.tableMask);
        if (total <= 0) {
            // 查询结果为空
            ElementUtils.console(">> 验证查询结果为空");
            assertEquals("暂无数据", ElementUtils.cssSelector(this.table, "span[class='el-table__empty-text']").getText());
            assertEquals("display: none;", this.pagination.getAttribute("style"));
        } else {
            ElementUtils.console(String.format(">> 验证查询结果校验总数=%s和共%d页", total, pages));
            UiAssertHelper.assertPageTotalAndPages(this.pagination, total, pages);
        }
        if (reset) {
            // 重置查询条件
            UiTriggerHelper.clickResetButton(this.searchForm, this.tableMask);
        }
    }

    /**
     * 查询结果测试
     *
     * @throws InterruptedException Sleep异常
     */
    public void searchTest() throws InterruptedException {
        // 1.校验总数和页码
        UiTriggerHelper.clickResetButton(this.searchForm, this.tableMask);
        ElementUtils.console(">> 验证校验总数19和共2页");
        UiAssertHelper.assertPageTotalAndPages(this.pagination, 19, 2);

        // 2.执行条件查询：字典名称=任务，字典类型=job，状态=正常
        this.search("任务", "job", 2, 1, true);
//        ElementUtils.sendKeysByCssSelector(this.searchForm, "input[placeholder='请输入字典名称']", "任务");
//        ElementUtils.sendKeysByCssSelector(this.searchForm, "input[placeholder='请输入字典类型']", "job");
//        WebElement elSelect = ElementUtils.cssSelector(this.searchForm, "div[class~='el-select']");
//        UiTriggerHelper.elSelectChange(elSelect, 0);
//        UiTriggerHelper.clickSearchButton(this.searchForm, this.tableMask);
//        ElementUtils.console(">> 验证查询结果校验总数2和共1页");
//        UiAssertHelper.assertPageTotalAndPages(this.pagination, 2, 1);
//        // 重置查询条件
//        UiTriggerHelper.clickResetButton(this.searchForm, this.tableMask);


        // 3.执行查询条件：字典名称=不存在的结果
        this.search("不存在的结果", "不存在的结果", 0, 1, true);
//        ElementUtils.sendKeysByCssSelector(this.searchForm, "input[placeholder='请输入字典名称']", "不存在的结果");
//        UiTriggerHelper.clickSearchButton(this.searchForm, this.tableMask);
//        // 查询结果为空
//        ElementUtils.console(">> 验证查询结果为空");
//        assertEquals("暂无数据", ElementUtils.cssSelector(this.table, "span[class='el-table__empty-text']").getText());
//        assertEquals("display: none;", this.pagination.getAttribute("style"));
    }

    /**
     * 新增测试
     *
     * @throws InterruptedException Sleep异常
     */
    public void insertTest() throws InterruptedException {
        this.search(dictName, dictCode, 0, 1, true);

        // 1.打开新增页面
        UiTriggerHelper.clickToolbarButton(this.toolbar, "新增", true);

        // 2. 填写信息：获取插入窗口
        WebElement insertDialog = ElementUtils.elDialog("添加字典类型");
        // 字典名称、字典值
        ElementUtils.sendKeysByCssSelector(insertDialog, "input[placeholder='请输入字典名称']", dictName);
        ElementUtils.sendKeysByCssSelector(insertDialog, "input[placeholder='请输入字典类型']", dictCode);
        // 确定按钮
        UiTriggerHelper.clickElDialogButton(insertDialog, "确 定");
        // 验证保存结果
        this.search(dictName, dictCode, 1, 1, false);
        UiAssertHelper.assertTableColumnResult(this.table, 2, new String[]{"Selenium字典"});
        UiAssertHelper.assertTableColumnResult(this.table, 3, new String[]{"selenium"});
    }

    /**
     * 修改测试
     *
     * @throws InterruptedException Sleep异常
     */
    public void updateTest() throws InterruptedException {
        // 1.修改页面：字典名称=用户性别，字典类型=sys_user_sex
        this.search(dictName, dictCode, 1, 1, false);

        // 查询结果只有一行
        List<WebElement> trs = ElementUtils.elTableTrs(this.table);
        assertEquals(1, trs.size());
        UiAssertHelper.assertTableColumnResult(this.table, 2, new String[]{dictName});
        UiAssertHelper.assertTableColumnResult(this.table, 3, new String[]{dictCode});
        WebElement optCol = ElementUtils.xpath(trs.get(0), "td[10]");
        // 点击修改按钮
        UiTriggerHelper.clickButton(optCol, "修改");
        ElementUtils.waitDialogVisibility();

        // 2. 填写信息：获取修改窗口
        WebElement updateDialog = ElementUtils.elDialog("修改字典类型");
        // 字典名称、字典值
        ElementUtils.sendKeysByCssSelector(updateDialog, "input[placeholder='请输入字典名称']", dictName + "Update");
        ElementUtils.sendKeysByCssSelector(updateDialog, "input[placeholder='请输入字典类型']", dictCode + "_update");
        // 确定按钮
        UiTriggerHelper.clickElDialogButton(updateDialog, "确 定");

        // 3.验证保存结果
        this.search(dictName, dictCode, 1, 1, false);

        // 查询结果只有一行
        trs = ElementUtils.elTableTrs(this.table);
        assertEquals(1, trs.size());
        UiAssertHelper.assertTableColumnResult(this.table, 2, new String[]{dictName + "Update"});
        UiAssertHelper.assertTableColumnResult(this.table, 3, new String[]{dictCode + "_update"});
    }

    /**
     * 删除测试
     *
     * @throws InterruptedException Sleep异常
     */
    public void deleteTest() throws InterruptedException {
        // 1.修改页面：字典名称=用户性别，字典类型=sys_user_sex
        this.search(dictName, dictCode, 1, 1, false);
        // 查询结果只有一行
        List<WebElement> trs = ElementUtils.elTableTrs(this.table);
        assertEquals(1, trs.size());

        // 2. 点击修改按钮
        WebElement optCol = ElementUtils.xpath(trs.get(0), "td[10]");
        UiTriggerHelper.clickButton(optCol, "删除");
        // 确定删除
        UiTriggerHelper.clickButton(ElementUtils.waitVisibility(ElementUtils.elPopover()), "确定");

        // 3.验证保存结果为空
        this.search(dictName, dictCode, 0, 1, true);
    }

}
